package games.ninJump.data
{
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import interfaces.IAction;
	import interfaces.IUpdate;
	
	public class TimeUpdate implements IAction
	{
		private static var UPDATE_OBJECTS:Vector.<IUpdate>;	 
		private var m_start:Boolean;
		private var m_now:Number;
		private var m_pause:Boolean;
		private var m_pauseTime:Number;
		
		public function TimeUpdate()
		{
			UPDATE_OBJECTS = new Vector.<IUpdate>();
		}
		
		public function set start(b:Boolean):void
		{
			m_start = b;
			if(b)
			{
				m_now = getTimer();
			}
		}
		
		public function get start():Boolean
		{
			return m_start;
		}
		
		public function set pause(b:Boolean):void
		{
			m_pause = b;
			if(b)
			{
				m_pauseTime = getTimer();
			}else
			{
				m_now += (getTimer() - m_pauseTime);
			}
		}
		
		public function get pause():Boolean
		{
			return m_pause;
		}
		
		public function update(e:Object = null):void
		{
			if(!start)return;
			if(pause)return;
			var t:Number = getTimer() - m_now;
			if(UPDATE_OBJECTS.length <= 0)return;
		 
			for(var i:int = 0; i < UPDATE_OBJECTS.length; i++)
			{
				UPDATE_OBJECTS[i].update(t);
			}
		}
		
		public function registerObj(obj:IUpdate):void
		{
			if(UPDATE_OBJECTS.indexOf(obj) != -1)return;
			UPDATE_OBJECTS.push(obj);
		}
		
		public function unregisterObj(obj:IUpdate):void
		{
			var index:int = UPDATE_OBJECTS.indexOf(obj);
			if(index == -1)return;
			UPDATE_OBJECTS.splice(index,1);
		}
		
		
	}
}